package com.ruoyi.erp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.Project;
import com.ruoyi.erp.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.StaffingPoMapper;
import com.ruoyi.erp.domain.StaffingPo;
import com.ruoyi.erp.service.IStaffingPoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 外派项目POService业务层处理
 * 
 * @author sauger
 * @date 2022-03-22
 */
@Service
public class StaffingPoServiceImpl implements IStaffingPoService 
{
    @Autowired
    private StaffingPoMapper staffingPoMapper;
    @Autowired
    private IProjectService projectService;

    /**
     * 查询外派项目PO
     * 
     * @param id 外派项目PO主键
     * @return 外派项目PO
     */
    @Override
    public StaffingPo selectStaffingPoById(Integer id)
    {
        return staffingPoMapper.selectStaffingPoById(id);
    }

    /**
     * 查询外派项目PO列表
     * 
     * @param staffingPo 外派项目PO
     * @return 外派项目PO
     */
    @Override
    public List<StaffingPo> selectStaffingPoList(StaffingPo staffingPo)
    {
        return staffingPoMapper.selectStaffingPoList(staffingPo);
    }

    /**
     * 新增外派项目PO
     * 
     * @param staffingPo 外派项目PO
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStaffingPo(StaffingPo staffingPo)
    {
        staffingPo.setCreateTime(DateUtils.getNowDate());
        int ret = staffingPoMapper.insertStaffingPo(staffingPo);
        this.updateProjectPoInfo(staffingPo.getProjectId());
        return ret;
    }

    /**
     * 修改外派项目PO
     * 
     * @param staffingPo 外派项目PO
     * @return 结果
     */
    @Override
    @Transactional
    public int updateStaffingPo(StaffingPo staffingPo)
    {
        staffingPo.setUpdateTime(DateUtils.getNowDate());
        int compare = staffingPo.getBilledAmount().compareTo(staffingPo.getOrderAmount()) ;
        if (compare == 1){
            //结算金额超过了po金额
            throw new ServiceException("结算金额不能超过po金额");
        }else if(compare == 0){
            staffingPo.setIsFinish("Y");
        }
        if (staffingPo.getIsFinish().equals("Y")){
            staffingPo.setAbortAmount(staffingPo.getOrderAmount().subtract(staffingPo.getBilledAmount()));
        }else{
            staffingPo.setAbortAmount(BigDecimal.ZERO);
        }
        int ret = staffingPoMapper.updateStaffingPo(staffingPo);
        this.updateProjectPoInfo(staffingPo.getProjectId());
        return ret;
    }

    /**
     * 批量删除外派项目PO
     * 
     * @param ids 需要删除的外派项目PO主键
     * @return 结果
     */
    @Override
    public int deleteStaffingPoByIds(String ids)
    {
        return staffingPoMapper.deleteStaffingPoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除外派项目PO信息
     * 
     * @param id 外派项目PO主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteStaffingPoById(Integer id)
    {
        StaffingPo staffingPo = this.selectStaffingPoById(id);
        int ret = staffingPoMapper.deleteStaffingPoById(id);
        this.updateProjectPoInfo(staffingPo.getProjectId());
        return ret;
    }

    private void updateProjectPoInfo(Integer projectId)
    {
        Project project = projectService.selectProjectById(projectId);
        if (StringUtils.isNull(project)) throw new ServiceException("参数错误");
        StaffingPo sum = staffingPoMapper.getSumAmountByProject(projectId);
        project.setAbortAmount(sum.getAbortAmount());
        project.setAmount(sum.getOrderAmount());
        project.setBilledAmount(sum.getBilledAmount());
        project.setPayedAmount(sum.getPayedAmount());
        project.setPoEndDate(sum.getEndDate());
        projectService.updateProject(project);
    }
}
